<?php
/*
// OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
// all the essential functionalities required for any enterprise.
// Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com

// OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.

// OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along with this program;
// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301, USA
*/

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/models/eimadmin/Skills.php';
require_once ROOT_PATH . '/lib/models/hrfunct/EmpSkillAllowance.php';

class SkillAllowances {

	var $tableName = 'HS_HR_SKILL_ALLOWANCE';

	var $skill_alwId;
	var $skill_alwDesc;
	var $skill_alwName;
	var $skillCode;
	var $skill_alwSalary;
	var $arrayDispList;
	var $singleField;


	function SkillAllowances() {
	}

	function setSkill_AlwId($skill_alwId) {
		$this->skill_alwId = $skill_alwId;
	}

	function setSkill_AlwDescription($skill_alwDesc) {
		$this->skill_alwDesc = $skill_alwDesc;
	}

	function setSkill_AlwName($skill_alwName) {
		$this->skill_alwName = $skill_alwName;
	}

	function setSkillCode($skillCode) {
		$this->skillCode = $skillCode;
	}

	function setSkill_AlwSalary($skill_alwSalary) {
		$this->skill_alwSalary = $skill_alwSalary;
	}

	function getSkill_AlwId() {
		return $this->skill_alwId;
	}

	function getSkill_AlwName() {
		return $this->skill_alwName;
	}

	function getSkill_AlwDescription() {
		return $this->skill_alwDesc;
	}

	function getSkillCode() {
		return $this->skillCode;
	}

	function getSkill_AlwSalary() {
		return $this->skill_alwSalary;
	}

	function getListofSkillAllowances($pageNO,$schStr,$mode, $sortField = 0, $sortOrder = 'ASC') {

		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->passResultSetMessage($pageNO,$schStr,$mode, $sortField, $sortOrder);

		//echo $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		$skills = new Skills();
		while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0];
	    	$arrayDispList[$i][1] = $line[1];
	    	$skillName = $skills->filterSkills($line[2]); //通过技能编号获取该技能的名称
	    	$arrayDispList[$i][2] = $skillName[0][1];
	    	$arrayDispList[$i][3] = $line[3];
	    	$arrayDispList[$i][4] = $line[4];
	    	$i++;

	     }

	     if (isset($arrayDispList)) {
			return $arrayDispList;
		} else {
			$arrayDispList = '';
			return $arrayDispList;

		}
	}

	function countSkillAllowances($schStr,$mode) {

		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->countResultset($schStr,$mode);

		//echo $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$line = mysql_fetch_array($message2, MYSQL_NUM);

	    return $line[0];
	}

	function delSkillAllowances($arrList) {

		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		
		$empsklalw = new EmpSkillAllowance();
		$empsklalw->delBySkillAlw($arrList);

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_delete = 'true';
		$sql_builder->arr_delete = $arrFieldList;

		$sqlQString = $sql_builder->deleteRecord($arrList);

		//echo $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}


	function addSkillAllowances() {

		if ($this->_isDuplicateName()) {
			throw new SkillAllowancesException("Duplicate name", 1);
		}

		$tableName = 'hs_hr_skill_allowance';
		
		$arrFieldList[0] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[1] = 'SKILL_CODE';
		$arrFieldList[2] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_DESCRIPTION';

		//$this->skillId = UniqueIDGenerator::getInstance()->getNextID($tableName, 'skill_allowance_id', 'SKI_ALW');
		//$arrFieldList[0] = "'". $this->getSkill_AlwId() . "'";
		$arrRecordsList[0] = "'". $this->getSkill_AlwName() . "'";
		$arrRecordsList[1] = "'". $this->getSkillCode() . "'";
		$arrRecordsList[2] = "'". $this->getSkill_AlwSalary() . "'";
		$arrRecordsList[3] = "'". $this->getSkill_AlwDescription() . "'";

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_insert = 'true';
		$sql_builder->arr_insert = $arrRecordsList;
		$sql_builder->arr_insertfield = $arrFieldList;

		$sqlQString = $sql_builder->addNewRecordFeature2();

		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}

	function updateSkillAllowances() {

		if ($this->_isDuplicateName(true)) {
			throw new SkillAllowancesException("Duplicate name", 1);
		}

		$this->getSkill_AlwId();
		$arrRecordsList[0] = "'". $this->getSkill_AlwId() . "'";
		$arrRecordsList[1] = "'". $this->getSkill_AlwName() . "'";
		$arrRecordsList[2] = "'". $this->getSkillCode() . "'";
		$arrRecordsList[3] = "'". $this->getSkill_AlwSalary() . "'";
		$arrRecordsList[4] = "'". $this->getSkill_AlwDescription() . "'";
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$tableName = 'HS_HR_SKILL_ALLOWANCE';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_update = 'true';
		$sql_builder->arr_update = $arrFieldList;
		$sql_builder->arr_updateRecList = $arrRecordsList;

		$sqlQString = $sql_builder->addUpdateRecord1();

		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		return $message2;
	}


	function filterSkillAllowances($getID) {

		$this->getID = $getID;
		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->selectOneRecordFiltered($this->getID);

		//echo $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		 while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0];
	    	$arrayDispList[$i][1] = $line[1];
	    	$arrayDispList[$i][2] = $line[2];
	    	$arrayDispList[$i][3] = $line[3];
	    	$arrayDispList[$i][4] = $line[4];
	    	$i++;

	     }

	    if (isset($arrayDispList)) {
			return $arrayDispList;
		} else {
			$arrayDispList = '';
			return $arrayDispList;
		}
	}

	function getSkillAllowances () {

		$sql_builder = new SQLQBuilder();
		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->passResultSetMessage(0, '', -1, 1);

		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$common_func = new CommonFunctions();

		$i=0;

		 while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0];
	    	$arrayDispList[$i][1] = $line[1];
	    	$arrayDispList[$i][2] = $line[2];
	    	$arrayDispList[$i][3] = $line[3];
	    	$arrayDispList[$i][4] = $line[4];


	    	$i++;
	     }

	     if (isset($arrayDispList)) {
	       	return $arrayDispList;
	     }
	}

	function filterGetSkillInfo($getID) {

		$this->getID = $getID;
		$tableName = 'HS_HR_SKILL';
		$arrFieldList[0] = 'SKILL_CODE';
		$arrFieldList[1] = 'SKILL_NAME';
		$arrFieldList[2] = 'SKILL_DESCRIPTION';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->selectOneRecordFiltered($this->getID);

		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		 while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0]; // Province Code
	    	$arrayDispList[$i][1] = $line[1]; // Provicne Name
	    	$arrayDispList[$i][2] = $line[2];

	    	$i++;
	     }

	     if (isset($arrayDispList)) {
			return $arrayDispList;
		} else {
			$arrayDispList = '';
			return $arrayDispList;
		}
	}

	function getUnAssSkillAllowances($id) {

		$sql_builder = new SQLQBuilder();
		$tableName = 'HS_HR_SKILL_ALLOWANCE';
		$arrFieldList[0] = 'SKILL_ALLOWANCE_ID';
		$arrFieldList[1] = 'SKILL_ALLOWANCE_NAME';
		$arrFieldList[2] = 'SKILL_CODE';
		$arrFieldList[3] = 'SKILL_ALLOWANCE_SALARY';
		$arrFieldList[4] = 'SKILL_ALLOWANCE_DESCRIPTION';

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;
		$sql_builder->field = 'SKILL_ALLOWANCE_ID';
		$sql_builder->field2 = 'SKILL_ALW_ID';
		$sql_builder->table2_name = 'HS_HR_EMP_SKILL_ALLOWANCE';

		$arr[0][0]='EMP_NUMBER';
		$arr[0][1]=$id;
		$sqlQString = $sql_builder->selectFilter($arr, 1, 1);

		//echo $sqlQString;exit;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		 while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0];
	    	$arrayDispList[$i][1] = $line[1];
	    	$arrayDispList[$i][2] = $line[2];
	    	$arrayDispList[$i][3] = $line[3];
	    	$i++;

	     }

	    if (isset($arrayDispList)) {
			return $arrayDispList;
		} else {
			$arrayDispList = '';
			return $arrayDispList;
		}
	}

	function filterNotEqualSubSkillInfo($getID) {

		$this->getID = $getID;

		$tableName = 'HS_HR_SKILL';
		$arrFieldList[0] = 'SKILL_CODE';
		$arrFieldList[1] = 'SKILL_NAME';
		$arrFieldList[2] = 'SKILL_DESCRIPTION';

		$sql_builder = new SQLQBuilder();

		$sql_builder->table_name = $tableName;
		$sql_builder->flg_select = 'true';
		$sql_builder->arr_select = $arrFieldList;

		$sqlQString = $sql_builder->filterNotEqualRecordSet($this->getID);

		//echo $sqlQString;
		$dbConnection = new DMLFunctions();
		$message2 = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function

		$i=0;

		 while ($line = mysql_fetch_array($message2, MYSQL_NUM)) {

	    	$arrayDispList[$i][0] = $line[0]; // Province Code
	    	$arrayDispList[$i][1] = $line[1]; // Provicne Name
	    	$arrayDispList[$i][2] = $line[2];

	    	$i++;
	     }

	     if (isset($arrayDispList)) {
			return $arrayDispList;
		} else {
			$arrayDispList = '';
			return $arrayDispList;
		}
	}

	private function _isDuplicateName($update=false) {
		$skillAllowances = $this->getListofSkillAllowances(0, $this->getSkill_AlwName(), 1);

		if (is_array($skillAllowances)) {
			if ($update) {
				if ($skillAllowances[0][0] == $this->getSkill_AlwId()) {
					return false;
				}
			}
			return true;
		}

		return false;
	}
}

class SkillAllowancesException extends Exception {
}
?>
